package data;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Enumeration;
import java.util.Properties;

/**
 * Class that handle Configuration data
 * @author lailiaidi
 */
public class ConfigManager {

    private Properties obConfigFile;
    private final String HARDCODE_ConfigPath = "sipspeaker.cfg";
    private final int HARDCODE_IntHTTPPort = 8080;
    private final int HARDCODE_IntSIPPort = 5060;
    private final String HARDCODE_StrHTTPAddress = "127.0.0.1";
    private final String HARDCODE_StrSIPAddress = "127.0.0.1";
    private final String HARDCODE_StrSIPUser = "robot";
    private final String HARDCODE_DefaultMessageText = "Welcome to the SIP Speaker this is my own answering machine. You have no new messages.";
    private final String HARDCODE_DefaultVoiceName = "default.wav";
    public final String HARDCODE_CurrentVoiceName = "currentmessage.wav";

    /*
     * Open the application configuration file NOT YET--> Check obStrConfigPath
     * is relative or abosolute path or not
     */
    public ConfigManager(String obStrConfigPath) {
        this.obConfigFile = new Properties();
        this.obStrConfigPath = obStrConfigPath;
        openProperty();
    }
    private String obStrConfigPath;

    private void openProperty() {
        try {
            FileInputStream in;
            // create and load default properties
            if (obStrConfigPath == null || obStrConfigPath.length() == 0) {
                System.out.println("No Configuration File is defined in command line, we try to use DEFAULT Configuration File in "+ HARDCODE_ConfigPath);
                in = new FileInputStream(HARDCODE_ConfigPath);
            } else {
                System.out.println("Try to Use Configuration File from command line parameter");
                in = new FileInputStream(obStrConfigPath);
            }
            obConfigFile.load(in);
            in.close();
        } catch (Exception e) {
            System.err.println("ERROR: Unable to Open configuration file, we use default value now");
        }
    }

    public void commitPropery(String obStrKey, String obStrValue) {
        try {
            Properties newProps = new Properties();
            Enumeration enProps = obConfigFile.propertyNames();
            String key = "";

            while (enProps.hasMoreElements()) {

                key = (String) enProps.nextElement();

                if (key.equals(obStrKey)) {
                    newProps.setProperty(key, obStrValue);
                } else {
                    newProps.setProperty(key, obConfigFile.getProperty(key));
                }
            }

            FileOutputStream out = new FileOutputStream(HARDCODE_ConfigPath);
            newProps.store(out, "---No Comment---"); // save?
            out.close();
            openProperty();

        } catch (Exception e) {
            System.err.println("ERROR: Unable to Open configuration file, we use default value now");
        }
    }

    public Properties getConfigFile() {
        return obConfigFile;
    }

    public synchronized void setHTTPHost(String obStrTCPHost) {
        if (obStrTCPHost != null || obStrTCPHost.length() > 0) {
            obConfigFile.setProperty("http_interface", obStrTCPHost);
            commitPropery("http_interface", obStrTCPHost);
        }
    }

    public synchronized String getHTTPHost() {
        String obStrTCPHost = HARDCODE_StrHTTPAddress;
        try {
            obStrTCPHost = obConfigFile.getProperty("http_interface").trim();
            System.out.println("CONFIG HTTPHost = " + obStrTCPHost);
            if (obStrTCPHost == null || obStrTCPHost.length() == 0) {
                obStrTCPHost = HARDCODE_StrHTTPAddress;
                System.out.println("HARDCODE HTTPHost = " + obStrTCPHost);
            }
        } catch (Exception e) {
            obStrTCPHost = HARDCODE_StrHTTPAddress;
            System.out.println("HARDCODE HTTPHost = " + obStrTCPHost);
        }
        return obStrTCPHost;
    }

    public synchronized void setHTTPPort(int obIntTCPPort) {
        String obStrTCPPort = Integer.toString(obIntTCPPort);
        if (obStrTCPPort != null
                && (obStrTCPPort.length() == 4 || obStrTCPPort.length() == 2)) {
            obConfigFile.setProperty("http_port", obStrTCPPort);
            commitPropery("http_port", obStrTCPPort);
        }
    }

    public synchronized int getHTTPPort() {
        int obIntTCPPort = HARDCODE_IntHTTPPort;
        try {
            String obStrTCPPort = obConfigFile.getProperty("http_port").trim();
            if (obStrTCPPort == null || obStrTCPPort.length() < 2) {
                obIntTCPPort = HARDCODE_IntHTTPPort;
                System.out.println("HARDCODE IntHTTPPort = " + obIntTCPPort);
            } else {
                try {
                    obIntTCPPort = Integer.parseInt(obStrTCPPort);
                    System.out.println("CONFIG IntHTTPPort = " + obIntTCPPort);
                } catch (Exception ex) {
                    obIntTCPPort = HARDCODE_IntHTTPPort;
                    System.out.println("HARDCODE IntHTTPPort = " + obIntTCPPort);
                }
            }
        } catch (Exception e) {
            obIntTCPPort = HARDCODE_IntHTTPPort;
            System.out.println("HARDCODE IntHTTPPort = " + obIntTCPPort);
        }
        return obIntTCPPort;
    }

    public synchronized void setSIPHost(String obStrSIPHost) {
        if (obStrSIPHost != null || obStrSIPHost.length() > 0) {
            obConfigFile.setProperty("sip_interface", obStrSIPHost);
            commitPropery("sip_interface", obStrSIPHost);
        }
    }

    public synchronized String getSIPHost() {
        String obStrSIPHost = HARDCODE_StrSIPAddress;
        try {
            obStrSIPHost = obConfigFile.getProperty("sip_interface").trim();
            System.out.println("CONFIG SIPHost = " + obStrSIPHost);
            if (obStrSIPHost == null || obStrSIPHost.length() <= 0) {
                obStrSIPHost = HARDCODE_StrSIPAddress;
                System.out.println("HARDCODE SIPHost = " + obStrSIPHost);
            }
        } catch (Exception e) {
            obStrSIPHost = HARDCODE_StrSIPAddress;
            System.out.println("HARDCODE SIPHost = " + obStrSIPHost);
        }
        return obStrSIPHost;
    }

    public synchronized void setSIPPort(int obIntUDPPort) {
        String obStrUDPPort = Integer.toString(obIntUDPPort);
        if (obStrUDPPort != null
                && (obStrUDPPort.length() == 4 || obStrUDPPort.length() == 2)) {
            obConfigFile.setProperty("sip_port", obStrUDPPort);
            commitPropery("sip_port", obStrUDPPort);
        }
    }

    public synchronized int getSIPPort() {
        int obIntUDPPort = HARDCODE_IntSIPPort;
        try {
            String obStrUDPPort = obConfigFile.getProperty("sip_port").trim();
            if (obStrUDPPort == null || obStrUDPPort.length() < 2) {
                obIntUDPPort = HARDCODE_IntSIPPort;
                System.out.println("HARDCODE SIPPort = " + obIntUDPPort);
            } else {
                try {
                    obIntUDPPort = Integer.parseInt(obStrUDPPort);
                    System.out.println("CONFIG SIPPort = " + obIntUDPPort);
                } catch (Exception ex) {
                    obIntUDPPort = HARDCODE_IntSIPPort;
                    System.out.println("HARDCODE SIPPort = " + obIntUDPPort);
                }
            }
        } catch (Exception e) {
            obIntUDPPort = HARDCODE_IntSIPPort;
            System.out.println("HARDCODE SIPPort = " + obIntUDPPort);
        }
        return obIntUDPPort;
    }

    public synchronized void setSIPUser(String obStrSIPUser) {
        if (obStrSIPUser != null || obStrSIPUser.length() > 0) {
            obConfigFile.setProperty("sip_user", obStrSIPUser);
            commitPropery("sip_user", obStrSIPUser);
        }
    }

    public synchronized String getSIPUser() {
        String obStrSIPUser = HARDCODE_StrSIPUser;
        try {
            obStrSIPUser = obConfigFile.getProperty("sip_user").trim();
            System.out.println("CONFIG SIPUser = " + obStrSIPUser);
            if (obStrSIPUser == null || obStrSIPUser.length() == 0) {
                obStrSIPUser = HARDCODE_StrSIPUser;
                System.out.println("HARDCODE SIPUser = " + obStrSIPUser);
            }
        } catch (Exception e) {
            obStrSIPUser = HARDCODE_StrSIPUser;
            System.out.println("HARDCODE SIPUser = " + obStrSIPUser);
        }
        return obStrSIPUser;
    }

    public synchronized void setDefaultVoiceName(String obDefaultVoice) {
        if (obDefaultVoice != null || obDefaultVoice.length() > 0) {
            obConfigFile.setProperty("default_message", obDefaultVoice);
            commitPropery("default_message", obDefaultVoice);
        }
    }

    public synchronized String getDefaultVoiceName() {
        String obDefaultVoice = HARDCODE_DefaultVoiceName;
        try {
            obDefaultVoice = obConfigFile.getProperty("default_message").trim();
            if (obDefaultVoice == null || obDefaultVoice.length() == 0) {
                obDefaultVoice = HARDCODE_DefaultVoiceName;
            }
        } catch (Exception e) {
            obDefaultVoice = HARDCODE_DefaultVoiceName;
        }
        return obDefaultVoice;
    }

    public synchronized void setCurrentVoiceName(String obConfigVoice) {
        if (obConfigVoice != null) {
            obConfigFile.setProperty("message_wav", obConfigVoice);
            commitPropery("message_wav", obConfigVoice);
        }
    }

    public synchronized String getCurrentVoiceName() {
        String obConfigVoice = null;
        try {
            obConfigVoice = obConfigFile.getProperty("message_wav").trim();
            if (obConfigVoice.length() == 0) {
                obConfigVoice = null;
            }
        } catch (Exception e) {
            obConfigVoice = null;
        }
        return obConfigVoice;
    }

    public synchronized void setCurrentMessage(String obConfigMessage) {
        if (obConfigMessage != null) {
            obConfigFile.setProperty("message_text", obConfigMessage);
            commitPropery("message_text", obConfigMessage);
        }
    }

    public synchronized String getDefaultMessage() {
        return HARDCODE_DefaultMessageText;
    }

    public synchronized String getCurrentMessage() {
        String obConfigMessage = null;
        try {
            obConfigMessage = obConfigFile.getProperty("message_text").trim();
            if (obConfigMessage.length() == 0) {
                obConfigMessage = null;
            }
        } catch (Exception e) {
            obConfigMessage = null;
        }
        return obConfigMessage;
    }

    public synchronized void setCurrentVoiceDate(String obConfigDate) {
        if (obConfigDate != null) {
            obConfigFile.setProperty("message_recived", obConfigDate);
            commitPropery("message_recived", obConfigDate);
        }
    }

    public synchronized String getCurrentVoiceDate() {
        String obConfigDate;
        try {
            obConfigDate = obConfigFile.getProperty("message_recived").trim();
            if (obConfigDate.length() == 0) {
                obConfigDate = null;
            }
        } catch (Exception e) {
            obConfigDate = null;
        }
        return obConfigDate;
    }
}// Class end

